import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class Niveau extends JPanel implements KeyListener, MouseListener {

	public JoueurModel joueurModel ;
//	public List<Entite> entites = new ArrayList<Entite>();
	public int xSpawn;
	public int ySpawn;
	public JFrame jfFrame;
	boolean keyUp=false, keyDown=false, keyLeft=false, keyRight=false;
	public int mx = 0, my = 0;
	private Toolkit tk = Toolkit.getDefaultToolkit();
	private Cursor Curseur;
	
	public Niveau(JFrame jfFrame){
		super();
		this.jfFrame=jfFrame;
		this.setBackground(Color.BLACK);
		this.joueurModel=new JoueurModel("Joueur");
		this.xSpawn = this.getWidth()/2;
		this.ySpawn= this.getHeight()/2;
		this.setFocusable(true);
		this.jfFrame.requestFocus();
		this.jfFrame.addKeyListener(this);
		this.jfFrame.addMouseListener(this);
	}
	
	protected void paintComponent( Graphics g){
		super.paintComponent(g);
		g.setColor(Color.RED);
		g.drawImage(joueurModel.getImage(), joueurModel.getX(), joueurModel.getY(), null);
		Curseur = tk.createCustomCursor( joueurModel.getMire(), new Point( 1, 1 ), "Pointeur" );
		setCursor( Curseur );
	}
	
	public JPanel getJPanel(){
		return this;
	}



	@Override
	public synchronized void keyPressed(KeyEvent arg0) {
		int touche = arg0.getKeyCode();
		if(touche == KeyEvent.VK_UP && touche != KeyEvent.VK_DOWN ){
			keyUp=true;
		}
		if(touche == KeyEvent.VK_DOWN && touche != KeyEvent.VK_UP  ){
			keyDown=true;
		}
		if(touche == KeyEvent.VK_RIGHT && touche != KeyEvent.VK_LEFT  ){
			keyRight=true;
		}
		if(touche == KeyEvent.VK_LEFT && touche != KeyEvent.VK_RIGHT  ){
			keyLeft=true;
		}
		
		prochainePos();
	}



	private void prochainePos() {
		
		if(keyLeft){
			if(joueurModel.getX()>0) joueurModel.seDeplacerGauche();
		}else if(keyRight){
			if(joueurModel.getX()<getWidth()-1-joueurModel.getImage().getWidth(null)) joueurModel.seDeplacerDroite();
		}
		if(keyDown){
			if(joueurModel.getY()<getHeight()-1-joueurModel.getImage().getHeight(null)) joueurModel.seDeplacerBas();
		} else if(keyUp){
			if(joueurModel.getY()>0) joueurModel.seDeplacerHaut();
		}
		repaint();
	}


	@Override
	public synchronized void keyReleased(KeyEvent arg0) {
		if(arg0.getKeyCode() == KeyEvent.VK_UP) keyUp = false; 
		if(arg0.getKeyCode() == KeyEvent.VK_DOWN) keyDown = false; 
		if(arg0.getKeyCode() == KeyEvent.VK_LEFT) keyLeft = false; 
		if(arg0.getKeyCode() == KeyEvent.VK_RIGHT) keyRight = false; 
		prochainePos();
	}



	@Override
	public void keyTyped(KeyEvent arg0) {
		
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		joueurModel.tirer();
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}



	
	
}
